﻿@using hc.epm.ViewModel;
@model ConstituteView
@{
    ViewBag.Title = "检查项添加";
}
<style>
    .btn-group {
        margin-top: 0px !important;
        text-align: left !important;
    }

    .datalist tr td {
        padding: 2px;
    }

        .datalist tr td input[type="text"] {
            width: 100%;
            height: 100%;
            border: 1px solid #ccc;
            box-sizing: border-box;
        }

    .selectpicker {
        width: 100%;
        height: 100%;
    }
</style>
<div class="container" style="margin-top:30px;margin-bottom:20px">
    <div class="row">
        <form class="form-horizontal bv-form" id="mainForm">
            <div class="col-lg-12 col-sm-12 mb25">
                <div class="form-group ">
                    <label for="parentName" class="col-sm-2 control-label"><em class="required">*</em>项目性质：</label>
                    <div class="col-sm-8">
                        @Html.DropDownList("ProjectNatureCode", null, new { @class = "form-control selectpicker  show-tick" })
                        <input type="hidden" name="ProjectNatureName" id="ProjectNatureName" value="@Model.Constitute.ProjectNatureName" />
                        <input type="hidden" name="Id" id="Id" value="@Model.Constitute.Id" />
                    </div>
                </div>
                <div class="form-group ">
                    <label for="parentName" class="col-sm-2 control-label"><em class="required">*</em>批复构成：</label>
                    <div class="col-sm-8">
                        @Html.DropDownList("ConstituteKey", null, new { @class = "form-control selectpicker  show-tick" })
                        <input type="hidden" name="ConstituteName" id="ConstituteName" value="@Model.Constitute.ConstituteName" />
                    </div>
                </div>
                <div class="form-group ">
                    <label for="name" class="col-sm-2 control-label">是否计费：</label>
                    <div class="col-sm-8">
                        <input type="hidden" name="IsAProvide" id="IsAProvide" value="false" />
                        @if (Model.Constitute.IsCharging == true)
                        {
                            <input type="checkbox" name="IsCharging" id="IsCharging" checked><span>是</span>
                        }
                        else
                        {
                            <input type="checkbox" name="IsCharging" id="IsCharging"><span>是</span>
                        }
                    </div>
                </div>
                <div class="form-group ">
                    <label for="sort" class="col-sm-2 control-label">排序：</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="Sort" name="Sort" value="@Model.Constitute.Sort" placeholder="请输入阶段排序">
                    </div>
                </div>
                <div class="form-group">
                    <label for="sort" class="col-sm-2 control-label">工程内容要点:</label>
                    <div class="col-sm-8">
                        <table class="layui-table datalist" style="width:100%;text-align:center;">
                            <thead>
                                <tr>
                                    <th width="40%">工程内容要点</th>
                                    <th width="20%">是否计费</th>
                                    <th width="15%">排序</th>
                                    <th width="20%">操作&nbsp;&nbsp;&nbsp;<a href="javascript:" class="add">添加</a></th>
                                </tr>
                            </thead>
                            <tbody>
                                @if (Model.WorkMainPoints.Any())
                                {
                                    foreach (var item in Model.WorkMainPoints)
                                    {
                                        <tr>
                                            <td>
                                                @Html.DropDownList("WorkMainPointsKey", null, new { @class = "form-control selectpicker  show-tick" })
                                                <input type="hidden" class="Droplist" value="@item.WorkMainPointsKey" />
                                            </td>
                                            <td>
                                                @if (item.IsCharging == true)
                                                {
                                                    <input type="checkbox" checked><span>是</span>
                                                }
                                                else
                                                {
                                                    <input type="checkbox"><span>是</span>
                                                }
                                            </td>
                                            <td>
                                                <input type="text" value="@item.Sort" />
                                            </td>
                                            <td>
                                                <a href="javascript:" class="delete">删除</a>
                                            </td>
                                        </tr>
                                    }
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12 mb25" style="margin-top:57px">
                <div class="col-lg-6 col-sm-6" style="text-align:right">
                    <input type="button" name="submit" id="save" lay-submit lay-filter="sub" value="提&nbsp;&nbsp;&nbsp;交" class="layui-btn layui-btn-small button-base bg-save" />
                </div>
                <div class="col-lg-6 col-sm-6">
                    <input type="button" value="取&nbsp;&nbsp;&nbsp;消" class="cancel layui-btn layui-btn-small button-base bg-cancel ">
                </div>
            </div>
            <input type="hidden" name="WorkMainPoint" id="WorkMainPoint" />
            <input type="hidden" name="MainPoints" id="MainPoints" />
        </form>
    </div>
</div>
<script>
    $("#ProjectNatureCode").change(function () {
        var projectNatureName = $('#ProjectNatureCode option:selected').text();
        $("#ProjectNatureName").val(projectNatureName);

    });
    $("#ConstituteKey").change(function () {
        var constituteName = $('#ConstituteKey option:selected').text();
        $("#ConstituteName").val(constituteName);

    });
    //验收项目下拉框取得默认值
    var DroplistItem = $(".Droplist").parent();
    DroplistItem.each(function (index, item) {
        $(item).find(".selectpicker").val($(item).find(".Droplist").val())
    });
    window.onload = function () {
        $('#mainForm').bootstrapValidator({
            excluded: [':hidden'],//[':disabled', ':hidden', ':not(:visible)'] //设置隐藏组件可验证
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                ProjectNatureCode: {
                    validators: {
                        notEmpty: {
                            message: '项目性质不能为空'
                        }
                    }
                }, ConstituteKey: {
                    validators: {
                        notEmpty: {
                            message: '总批复构成不能为空'
                        }
                    }
                },
                Sort: {
                    validators: {
                        stringLength: {
                            max: 10,
                            message: '排序不能超过10字符'
                        }, regexp: {
                            regexp: /^[0-9]+$/i,
                            message: "排序由数字组成"
                        }
                    }
                }
            }
        });
    }
    layui.use(['layer', 'form', 'element', 'laypage'], function () {
        var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
        //监听提交
        form.on('submit(sub)', function (data) {
            //对表单进行验证
            var bv = $('#mainForm').data('bootstrapValidator');
            bv.validate();
            if (bv.isValid()) {
                debugger;
                var MainPoints = "";
                var WorkMainPoint = [];
                var pCode = $("#ProjectNatureCode").val();
                var pName = $("#ProjectNatureName").val();
                $("tbody tr").each(function (index, item) {
                    WorkMainPoint.push({
                        WorkMainPointsKey: $(item).children("td:eq(0)").find(".selectpicker").val(),
                        WorkMainPointsName: $(item).children("td:eq(0)").find(".selectpicker option:selected").text(),
                        Sort: $(item).children("td:eq(2)").children("input").val(),
                        IsCharging: $(item).children("td:eq(1)").find("input:checked").is(':checked') ? true : false,
                        ProjectNatureCode: pCode,
                        ProjectNatureName: pName,
                    })
                    MainPoints = MainPoints + $(item).children("td:eq(0)").find(".selectpicker option:selected").text() + ',';
                });
                WorkMainPoint = JSON.stringify(WorkMainPoint);
                $("#WorkMainPoint").val(WorkMainPoint);
                $("#MainPoints").val(MainPoints.substr(0, MainPoints.length - 1));
                if ($("#IsCharging").is(':checked')) {
                    $("#IsCharging").val(true);
                } else {
                    $("#IsCharging").val(false);
                }
                var search = $("form").serialize();
                $.post("Edit", search, function (data) {
                    if (data.Flag) {
                        parent.layer.msg("添加成功", { time: 1000, icon: 1 }, function () {
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);//关闭窗口
                            parent.layer.close(alertIndex);
                        });
                    } else {
                        layer.alert(data.Message, { icon: 2 });
                    }
                });
                return true;
            }
        });
        //关闭弹出iframe层
        $('.cancel').on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);//关闭窗口
        });
        //添加验收项目
        $('.datalist').on('click', '.add', function () {
            var newTime = new Date().getTime();
            var tr = `
                        <tr>
                            <td>
                                @Html.DropDownList("WorkMainPointsKey", null, new { @class = "form-control selectpicker  show-tick" })
                            </td>
                            <td>
                                <input type="checkbox" class ="rec_result"/>是&nbsp; &nbsp; &nbsp;
                            </td>
                            <td>
                                <input type="text" />
                            </td>
                            <td>
                                <a href="javascript:" class ="delete">删除</a>
                            </td>
                        </tr>
                        `
            $('.datalist tbody').append(tr);
        })
        //删除验收项目
        $(".datalist").on("click", ".delete", function () {
            $(this).parent().parent().remove();
        })
    });
</script>